//
//  HOTPopupView.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-10-9.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTPopupView.h"

@interface HOTPopupView () {
    
    UIView *_inView;
    UIView *mengban;
}

@end

@implementation HOTPopupView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithTitle:(NSString *)title message:(NSString *)message {
    
    HOTPopupView *selfObj = [self initView];
    
    selfObj->titleLabel.text = title;
    selfObj->contentLabel.text = message;
    
    return selfObj;
}

-(HOTPopupView *)initView {
    
    //加载xib
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HOTPopupView" owner:self options:nil];
    
    HOTPopupView *selfObj = [nibs firstObject];
    
    selfObj.animationDuration = 0.3f;
    
    return selfObj;
}

/**
 *  显示在指定的view中
 *
 *  @param superView 指定显示的父view对象
 */
-(void)showInView:(UIView *)superView {
    
    _inView = superView;
    
    
    [self showPopViewInView:superView];
}

- (IBAction)okBtnClicked:(id)sender {
    
    if (self.popBtnClickedBlock) {
        
        self.popBtnClickedBlock(HOTPopOKBtn);
    }
    
    [self hiddenPopupView];
}

- (IBAction)cancelBtnClicked:(id)sender {
    
    if (self.popBtnClickedBlock) {
        
        self.popBtnClickedBlock(HOTPopCancelBtn);
    }
    [self hiddenPopupView];
}

/**
 *  显示在window对象的窗口中
 */
-(void)showInWindow {
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    
    _inView = keyWindow;
    
    [self showPopViewInView:keyWindow];
}

/**
 *  显示弹出框的共通方法
 *
 *  @param superView 指定显示的view对象
 */
-(void)showPopViewInView:(UIView *)superView {
    
    self.center = superView.center;
    self.center = CGPointMake(self.center.x + self.offsetPoint.x, self.center.y + self.offsetPoint.y);
    
    if (mengban == nil) {
        mengban = [[UIView alloc] initWithFrame:CGRectMake(0, 0, superView.frame.size.width, superView.frame.size.height)];
        mengban.backgroundColor = [UIColor blackColor];
        mengban.alpha = 0.f;
    }
    //self.view.alpha = 0;
    mengban.alpha = 0;
    self.alpha = 0;
    [superView addSubview:mengban];
    [superView addSubview:self];
    [superView bringSubviewToFront:self];
    
    [UIView beginAnimations:@"showMengban" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:self.animationDuration];
    
    self.alpha = 1;
    mengban.alpha = 0.5;
    
    [UIView commitAnimations];
}


/**
 *  隐藏弹出框处理
 */
-(void)hiddenPopupView {
    //self.view.alpha = 0;

    __unsafe_unretained HOTPopupView *unretainObj = self;
    [UIView animateWithDuration:self.animationDuration animations:^{
        
        mengban.alpha = 0;
        unretainObj.alpha = 0;
    } completion:^(BOOL finished) {
        [unretainObj removeFromSuperview];
        [mengban removeFromSuperview];
    }];

}

@end
